/*
 * @desc:
 * @Author: 钟强
 * @Date: 2021-12-22 14:22:30
 * @LastEditors: 钟强
 * @LastEditTime: 2021-12-22 18:07:31
 */
import { publicRoutes, privateRoutes } from '@/router'

export default {
  namespaced: true,
  state: {
    routes: publicRoutes
  },
  mutations: {
    // 增加路由
    setRoutes (state, newRoutes) {
      state.routes = [...publicRoutes, ...newRoutes]
    }
  },
  actions: {
    // 根据权限筛选路由
    filterRoutes (context, menus) {
      // 路由权限匹配
      const routes = []
      menus.forEach(key => {
        routes.push(...privateRoutes.filter(item => item.name === key))
      })

      // 最好添加，不匹配路由进入404
      routes.push({
        path: '/:catchAll(.*)',
        redirect: '/404'
      })

      context.commit('setRoutes', routes)
      return routes
    }
  }
}
